<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BranchHelper');
$BranchHelper = new BranchHelper();

$this->LoadHelper('CityHelper');
$CityHelper = new CityHelper();

if (empty($this->Param['option'])) {
    $data = $BankHelper->bankRow($id);
    $data['bk_inpro'] = !empty($data['bk_inpro']) ? explode(',', $data['bk_inpro']) : array();
    $this->Assign('data', $data);
    $this->Assign('id', $id);
    echo $this->GetView('edit.php');
} else {
    switch ($this->Param['option']) {
        case 'save':
            $this->Param['bk_inpro'] = empty($this->Param['bk_inpro']) ? '' : implode(',', $this->Param['bk_inpro']);
            if($id > 0){
                $result = $BankHelper->bankSave($this->Param, array('`bk_id` = ?' => $id));
            }else{
                $this->Param['bk_dateline'] = NOW_TIME;
                $result = $BankHelper->bankSave($this->Param);
            }
            if($result['success']){
                $desc = $id > 0 ? '修改银行信息' : '新增银行信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $BankHelper->bankRemove($id);
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除银行', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        case 'branchlist':
            $bk_id = empty($this->Param['bk_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['bk_id'];
            $ajax = $this->Param['ajax'];
            $page = 1;
            $rp = 1000;
            if ($ajax) {
                $where['`br_bank` LIKE ?'] = '%'.$bk_id.'%';
                $bk_msg = $BankHelper->bankRow($bk_id);
                $data = $BankHelper->branList($where, $page, $rp, $this->Param);
                $json_data = array('total' => $data['count'], 'rows' => array());
                if (!empty($data['all'])) {
                    foreach ($data['all'] as $rowNum => $row) {
                        $row['br_rgid'] = $BranchHelper->getRegionname($row['br_rgid']);
                        $br_bank_arr = explode(",", $row['br_bank']);
                        $row['br_count'] = count($br_bank_arr);
                        $row['br_bank'] = $bk_msg['bk_name'];
                        $row['br_bank_arr'] = '';
                        $json_data['rows'][] = $row;
                    }
                }
                echo json_encode($json_data);
            } else {
                $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
                $this->Assign('member_info', $member_info);
                echo $this->GetView('bank_branchlist.php');
            }
            break;
        case 'closebank':
            $bk_id = empty($this->Param['bkid']) ? ErrorMsg::Debug('参数错误') : $this->Param['bkid'];
            $br_id = empty($this->Param['br_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['br_id'];
            if(is_numeric($bk_id)){
                $result = $BranchHelper->closebank($br_id,$bk_id);
            }else{
                $result = $BranchHelper->closeAll($br_id);
            }
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '关闭银行端口', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('端口关闭成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'openbank':
            $bk_id = empty($this->Param['bkid']) ? ErrorMsg::Debug('参数错误') : $this->Param['bkid'];
            $bk_msg = $BankHelper->bankRow($bk_id);
            $br_msg = $BranchHelper->nobankBranch($bk_id);
            $this->Assign('br_msg',$br_msg);
            $this->Assign('msg', $bk_msg);
            echo $this->GetView('bank_openport.php');
            break;
        case 'banksave':
            $br_name = empty($this->Param['br_name']) ? ErrorMsg::Debug('参数错误') : $this->Param['br_name'];
            $bk_id = empty($this->Param['bk_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['bk_id'];
            $result = $BranchHelper->openBank($br_name,$bk_id);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '开通银行端口', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('端口开通成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'admittancelist':
            $bank_id = empty($this->Param['bk_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['bk_id'];
            $all_data = $CityHelper->getAll(0,true);
            $filter_data = $CityHelper->getAll($bank_id,true);
            $this->Assign('all_data', $all_data);
            $this->Assign('filter_data', $filter_data);
            $this->Assign('bank_id',$bank_id);
            echo $this->GetView('bank_admittance.php');
            break;
        case 'admittancesave':
            $old_data = $CityHelper->takeoutId($this->Param['bank_id']);
            $new_data = empty($this->Param['place_id'])? array() : $this->Param['place_id'];
            $diff1 = empty($old_data)? array() : array_diff($old_data,$new_data);//关闭的
            $diff2 = empty($old_data)? $new_data : array_diff($new_data,$old_data);//新开的
            $result = $CityHelper->admittanceEdit($diff1,$diff2,$this->Param['bank_id']);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '银行上牌地抵押备案管理', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }else{
                ErrorMsg::Debug($result['msg']);
            }
            break;
        default:
    }
    
}